package neusoft.edu.cn.guanshangshiguang.fragments;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import neusoft.edu.cn.guanshangshiguang.R;
import neusoft.edu.cn.guanshangshiguang.activities.SearchActivity;

public class ReadFragment extends Fragment {

    private DisplayMetrics dm;
    private ViewPager viewPager;
    private PagerSlidingTabStrip pagerSlidingTabStrip;
    private ReadFragmentOnline readFragmentOnline = null;
    private ReadFragmentLibrary readFragmentLibrary = null;
    private ReadFragmentPersonal readFragmentPersonal = null;

    private TextView search;

    public ReadFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_read, container, false);

    }

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        init(view);

        setpagerstyle();//设置PagerSlidingTabStrip显示效果

        dm = getResources().getDisplayMetrics();//获取屏幕密度
        viewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        pagerSlidingTabStrip.setViewPager(viewPager);

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(),SearchActivity.class);
                startActivity(intent);
            }
        });

    }

    private void setpagerstyle() {
        pagerSlidingTabStrip.setDividerColor(Color.TRANSPARENT); // 设置Tab的分割线是透明的
        pagerSlidingTabStrip.setTabBackground(0); // 取消点击Tab时的背景色
    }

    private void init(View view) {
        viewPager = view.findViewById(R.id.viewPager);
        pagerSlidingTabStrip = view.findViewById(R.id.pagerslidingtabstrip);
        search = view.findViewById(R.id.Search);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter{

        private String[] titles = {"在线", "馆内","创作"};//显示在二级导航上的标题文字

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];//确定当页导航上文字
        }

        @Override
        public Fragment getItem(int i) {//根据位置返回具体某个导航上对应的Fragment
            switch (i){
                case 0:
                    if(readFragmentOnline==null)
                        readFragmentOnline = new ReadFragmentOnline();
                    return readFragmentOnline;

                case 1:
                    if(readFragmentLibrary==null)
                        readFragmentLibrary = new ReadFragmentLibrary();
                    return readFragmentLibrary;

                case 2:
                    if(readFragmentPersonal ==null)
                        readFragmentPersonal = new ReadFragmentPersonal();
                    return readFragmentPersonal;

                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return titles.length;//二级导航个数
        }
    }
}

